package pkg.calculadora;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Interface extends JFrame implements ActionListener, KeyListener{
	private double oper1=0;
	private double oper2=0;
	private String operacion;
	private JTextField txf=null;
	private boolean NuevaLinea=true;
	private Calculos calcular=null;
	
	public Interface()
	{
		Dimension pantalla = Toolkit.getDefaultToolkit().getScreenSize();
		this.setSize(300, 220);
		Dimension ventana = this.getSize();
		int width = (pantalla.width - ventana.width) / 2;
	    int height = (pantalla.height - ventana.height) / 2;
	    this.setLocation(width, height);
		this.setTitle("Calculadora");
		this.setResizable(false);
		this.addWindowListener(new Acabar());
		this.getContentPane().setLayout(new BorderLayout());
		
		txf = new JTextField();
		txf.setBackground(Color.white);
		txf.addKeyListener(this);
		txf.setHorizontalAlignment(JTextField.RIGHT);
		JPanel padre= new JPanel();
		padre.setLayout(new GridLayout(5,1));
		
		JPanel p2= new JPanel();
		p2.setLayout(new GridLayout(1,3));
		
		JPanel p3= new JPanel();
		p3.setLayout(new GridLayout(1,4));
		
		JPanel p4= new JPanel();
		p4.setLayout(new GridLayout(1,4));
		
		JPanel p5= new JPanel();
		p5.setLayout(new GridLayout(1,4));
		
		JPanel p1= new JPanel();
		p1.setLayout(new GridLayout(1,1));
		
		JButton b=null;
		
		b=new JButton("*");
		b.addActionListener(this);
		p2.add(b);
		
		b=new JButton("/");
		b.addActionListener(this);
		p2.add(b);
		
		b=new JButton("C");
		b.addActionListener(this);
		p2.add(b);
		
		b=new JButton("7");
		b.addActionListener(this);
		p3.add(b);
		
		b=new JButton("8");
		b.addActionListener(this);
		p3.add(b);
		
		b=new JButton("9");
		b.addActionListener(this);
		p3.add(b);
		
		b=new JButton("+");
		b.addActionListener(this);
		p3.add(b);
		
		b=new JButton("4");
		b.addActionListener(this);
		p4.add(b);
		
		b=new JButton("5");
		b.addActionListener(this);
		p4.add(b);
		
		b=new JButton("6");
		b.addActionListener(this);
		p4.add(b);
		
		b=new JButton("-");
		b.addActionListener(this);
		p4.add(b);
		
		b=new JButton("1");
		b.addActionListener(this);
		p5.add(b);
		
		b=new JButton("2");
		b.addActionListener(this);
		p5.add(b);
		
		b=new JButton("3");
		b.addActionListener(this);
		p5.add(b);
		
		b=new JButton("=");
		b.addActionListener(this);
		p5.add(b);
		
		b=new JButton("0");
		b.addActionListener(this);
		p1.add(b);
		p1.setSize(100, 20);
		padre.add(p2);
		padre.add(p3);
		padre.add(p4);
		padre.add(p5);
		padre.add(p1);
		
		this.getContentPane().add(txf, BorderLayout.NORTH);
		this.getContentPane().add(padre);
	}
	
	@Override
	public void keyPressed(KeyEvent e) {}

	@Override
	public void keyReleased(KeyEvent e) {
	}

	@Override
	public void keyTyped(KeyEvent e) {
		int k = (int) e.getKeyChar();
		if (!Character.isDigit(k)) { 
		e.consume();
		JOptionPane.showMessageDialog(null, "No puede ingresar Letras!!!", "Error Datos", JOptionPane.ERROR_MESSAGE);
		}
		else
		{
			txf.setText(txf.getText().substring(0, txf.getText().length()));
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String boton =e.getActionCommand();
		if(boton.equals("+")||boton.equals("-")||boton.equals("*")||boton.equals("/"))
		{
			operacion=boton;
			oper1= Integer.parseInt(txf.getText());
			NuevaLinea=true;
		}
		else if(boton.equals("="))
		{
			oper2=Integer.parseInt(txf.getText());
			calcular= new Calculos();
			txf.setText(new Double(calcular.calcular(operacion, oper1, oper2)).toString());
			NuevaLinea= true;
		}
		else if(boton.equals("C"))
		{
			txf.setText("");
		}
		else
		{
			if(NuevaLinea)
			{
			txf.setText(boton);
			NuevaLinea=false;
			}
			else
			{
				txf.setText(txf.getText()+ boton);
			}
		}
		
	}

	public class Acabar extends WindowAdapter
	{
		public void closingWindow(WindowEvent e)
		{
			System.exit(0);
		}
	}
}
